package com.jandan.web.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

import com.jandan.logic.JWordzFacade;
import com.jandan.ui.client.util.ClientUtil;
import com.jandan.ui.model.Word;
import com.jandan.ui.model.WordLib;

public class WordLibController extends MultiActionController{
    private JWordzFacade jwordz;
    private String wordLibImagePath;

    public void setJwordz(JWordzFacade jwordz) {
		this.jwordz = jwordz;
	}

	public void setWordLibImagePath(String wordLibImagePath) {
		this.wordLibImagePath = wordLibImagePath;
	}

	public ModelAndView displayAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String pageIndex = request.getParameter("pageIndex");
		int pi = ClientUtil.FIRST_PAGE_INDEX;
		int limit = ClientUtil.WORDLIB_PAGE_LIMIT;
		
		if(pageIndex != null){
			pi = Integer.parseInt(pageIndex);
		}
		int totalWordLibCount = jwordz.getTotalWordLibCount();
		List<WordLib> wordLibList = jwordz.getAllWordLibList(pi * limit,limit);
		if(wordLibList == null){
			wordLibList = new ArrayList<WordLib>();
		}
		
		Map map = new HashMap();
		map.put("totalWordLibCount", totalWordLibCount);
		map.put("wordLibList", wordLibList);
		
		return new ModelAndView("admin_wordlib",map);
	}
	
	public ModelAndView displayOne(HttpServletRequest request,HttpServletResponse reponse){
		String wordLibID = request.getParameter("wordLibID");
		ModelAndView model = this.getModelWithWordLib("admin_wordlibinfo",wordLibID);
		
		long id = Long.parseLong(wordLibID);
		List<Word> wordList = jwordz.getAllWordListByWordLibID(id);
		model.addObject("wordList", wordList);

		return model;
	}
	
	public ModelAndView displayForm(HttpServletRequest request,HttpServletResponse reponse){
		String wordLibID = request.getParameter("wordLibID");
		ModelAndView model = this.getModelWithWordLib("admin_editWordLibForm",wordLibID);
		
		return model;
	}
	
	public ModelAndView add(HttpServletRequest request,HttpServletResponse reponse){
		WordLib wordLib = generateWordLib(request);		
		jwordz.insertWordLib(wordLib);
		return new ModelAndView(new RedirectView("wordlib.htm"),"message","词库添加成功");
	}
	
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse reponse){
		WordLib wordLib = generateWordLib(request);
		jwordz.updateWordLib(wordLib);
		return new ModelAndView(new RedirectView("wordlib.htm"),"message","词库修改成功");
	}

	private WordLib generateWordLib(HttpServletRequest request) {
		String wordLibName = request.getParameter("wordLibName");
		int firstClass = Integer.parseInt(request.getParameter("firstClass"));
		int secondClass = Integer.parseInt(request.getParameter("secondClass"));
		int thirdClass = Integer.parseInt(request.getParameter("thirdClass"));
		int forthClass = Integer.parseInt(request.getParameter("forthClass"));
		String description = request.getParameter("description");
		String libImagePath = wordLibImagePath + request.getParameter("libImagePath");
		
		WordLib wordLib = new WordLib();
		String s = request.getParameter("wordLibID");
		if(s != null){
			wordLib.setWordLibID(Long.parseLong(s));
		}
		wordLib.setWordLibName(wordLibName);
		wordLib.setFirstClass(firstClass);
		wordLib.setSecondClass(secondClass);
		wordLib.setThirdClass(thirdClass);
		wordLib.setForthClass(forthClass);
		wordLib.setDescription(description);
		wordLib.setLibImagePath(libImagePath);
		return wordLib;
	}
	
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse reponse){
		long wordLibID = Long.parseLong(request.getParameter("wordLibID"));
		jwordz.deleteWordLib(wordLibID);
		return new ModelAndView(new RedirectView("wordlib.htm"),"message","词库删除成功");
	}
	
	public ModelAndView importWords(HttpServletRequest request,HttpServletResponse response){
		String wordLibID = request.getParameter("wordLibID");
		ModelAndView model = this.getModelWithWordLib("admin_importwords",wordLibID);
		return model;		
	}
	
	private ModelAndView getModelWithWordLib(String viewName,String wordLibID){
		ModelAndView model = new ModelAndView(viewName);
		if(wordLibID != null){
			long id = Long.parseLong(wordLibID);
			WordLib wordLib = jwordz.getWordLibByWordLibID(id);
			model.addObject("wordLib", wordLib);
		}
		return model;
	}
	
	public ModelAndView displayWordForm(HttpServletRequest request,HttpServletResponse reponse){
		ModelAndView model = new ModelAndView("admin_editWordForm");
		String wordLibID = request.getParameter("wordLibID");
		String wordID = request.getParameter("wordID");
		
		if(wordID != null){
			long id = Long.parseLong(wordID);
			Word word = jwordz.getWordByWordID(id);
			model.addObject("word", word);
		}
		
		model.addObject("wordLibID",wordLibID);
		
		return model;
	}
	
	public ModelAndView deleteWord(HttpServletRequest request,HttpServletResponse reponse){
		long wordLibID = Long.parseLong(request.getParameter("wordLibID"));
		long wordID = Long.parseLong(request.getParameter("wordID"));
		jwordz.deleteWord(wordID);
		return new ModelAndView(new RedirectView("wordlib.htm?method=displayOne&wordLibID=" + wordLibID),"message","单词删除成功");
	}
	
	public ModelAndView deleteCnAndSentence(HttpServletRequest request,HttpServletResponse reponse){
		long csID = Long.parseLong(request.getParameter("csID"));
		jwordz.deleteCnAndSentence(csID);
		return new ModelAndView();
	}
}
